package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.pms.pojo.PmsSkuStock;
import com.imti.mall.product.service.PmsSkuStockService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/product")
@Api(tags = "商品Sku信息")
public class PmsSkuStockController {
    @Autowired
    PmsSkuStockService pmsSkuStockService;

    /**
     *根据商品id查询SKU
     * @param id
     * @return
     */
    @GetMapping ("/getPmsSkuStockById/{id}")
    public CommonResult getPmsSkuStockById(@PathVariable("id") Integer id){
        List<PmsSkuStock> pmsSkuStockproductProductId = pmsSkuStockService.getPmsSkuStockproductProductId(id);
        return CommonResult.success(pmsSkuStockproductProductId);
    }

    /**
     *根据商品id和Sku编号查询SKU信息
     * @param id
     * @return
     */
    @GetMapping("/getPmsSkuStockproductProductSkuCode/{id}")
    public CommonResult getPmsSkuStockproductProductSkuCode(@PathVariable("id") Integer id, String keyword){
        List<PmsSkuStock> pmsSkuStockproductProductSkuCode=new ArrayList<>();
        if(keyword==null){
             pmsSkuStockproductProductSkuCode = pmsSkuStockService.getPmsSkuStockproductProductId(id);
        }else {
            PmsSkuStock pmsSkuStockproductProductSku = pmsSkuStockService.getPmsSkuStockproductProductSkuCode(id, keyword);
            pmsSkuStockproductProductSkuCode.add(pmsSkuStockproductProductSku);
        }

        return CommonResult.success(pmsSkuStockproductProductSkuCode);
    }



    /**
     * 根据id修改查询SKU数据
     * @param pmsSkuStock
     * @return
     */
    @PostMapping("/updateSkuStockById")
    public CommonResult updateSkuStockById(@RequestBody List<PmsSkuStock> pmsSkuStock) {
        Integer integer = 0;
        for(int i=0;i<pmsSkuStock.size();i++){
            integer=pmsSkuStockService.updateSkuStockById(pmsSkuStock.get(i));
        }

        if(integer>0){
            return CommonResult.failed("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }
    }


    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @RequestMapping("/delSkuStockById/{id}")
    public CommonResult delSkuStockById( @PathVariable("id") Integer id) {
        if(pmsSkuStockService.delSkuStockById(id)>0){
            return CommonResult.failed("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }

    }

    /**
     * 添加sku信息
     * @param pmsSkuStock
     * @return
     */
    @RequestMapping("/addSkuStockById")
    public CommonResult addSkuStockById(PmsSkuStock pmsSkuStock) {
        if(pmsSkuStockService.addSkuStockById(pmsSkuStock)>0){
            return CommonResult.failed("添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }

    }

}
